package com.econvent.common.domain;

import com.econvent.common.annotation.NotPrint;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @Entity基类
 * 
 * @author xiekun
 * @version 1.0
 * */
public class BaseEntity<T> implements Serializable {

    /**
	 * 
	 */
    private static final long serialVersionUID = 7750784001160387378L;

    /**
     * 重写toString 方法，将java Object  以JSON的形式输出
     *
     * @return string
     */
    @Override
    public String toString() {
        ReflectionToStringBuilder.setDefaultStyle(ToStringStyle.JSON_STYLE);

        Field fieldArray[] = this.getClass().getDeclaredFields();
        List<String> excludeFieldsName = new ArrayList<String>();

        for (Field field : fieldArray) {
            Annotation annotationArray[] = field.getDeclaredAnnotations();
            for (Annotation annotation : annotationArray) {
                if(annotation.annotationType().equals(NotPrint.class)){
                    excludeFieldsName.add(field.getName());
                }
            }
        }
        return ReflectionToStringBuilder.toStringExclude(this, excludeFieldsName);
    }
}
